#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash BerkeleyGW-3.0.1.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y  environment-modules time patch libatomic  wget

set -e
cp src/BerkeleyGW-3.0.1.tar.gz $buildpath
tar xf $buildpath/BerkeleyGW-3.0.1.tar.gz -C $buildpath && rm -rf $buildpath/BerkeleyGW-3.0.1.tar.gz
cp arch.mk $buildpath/BerkeleyGW-3.0.1
cd $buildpath/BerkeleyGW-3.0.1

sed -i "21c MPI_PATH = ${installpath}/hmpi-1.1.1/hmpi-1.1.1-huawei/bin" arch.mk
sed -i "40c FFTW_DIR = ${installpath}/fftw-3.3.10" arch.mk
sed -i "43c FFTWLIB = ${installpath}/fftw-3.3.10/lib/libfftw3.a" arch.mk
sed -i "47c SCALAPACK_DIR = ${installpath}/scalapack-2.1.0/lib" arch.mk
sed -i "51c HDF5DIR = ${installpath}/hdf5-1.12.1" arch.mk
make all-flavors
make install INSTDIR=${installpath}/BerkeleyGW-3.0.1

cat>"$installpath/BerkeleyGW-3.0.1/berkeley_modulefiles"<<EOF
#%Module1.0
conflict berkeley
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set BERKELEY \$pwd
setenv BERKELEY \$BERKELEY
prepend-path PATH \$BERKELEY/bin
EOF

rm -rf $buildpath/BerkeleyGW-3.0.1

